using System;
using System.Data;
using System.Data.SqlClient;

namespace ClienteCaptura {
  public class DatabaseHelper {

    public static SqlDataReader ExecuteReader(string command_text) {
      return ExecuteReader(GetConnectionString(), command_text);
    }

    public static SqlDataReader ExecuteReader(string connection_string, string command_text) {

      SqlConnection connection = new SqlConnection(connection_string);
      SqlCommand command = new SqlCommand(command_text, connection);
      command.CommandType = CommandType.Text;

      try {
        connection.Open();

        return (command.ExecuteReader(CommandBehavior.CloseConnection));
      }
      catch {
        connection.Close();
        throw;
      }
    }

    public static DataSet ExecuteCommand(string command_text) {
      return ExecuteCommand(GetConnectionString(), command_text);
    }
      
    public static DataSet ExecuteCommand(string connection_string, string command_text) {
  
      using(SqlConnection connection = new SqlConnection(connection_string)) {
        connection.Open();
        
        SqlCommand command = new SqlCommand(command_text, connection);
        command.CommandType = CommandType.Text;
        
        SqlDataAdapter adapter = new SqlDataAdapter(command);
        DataSet result = new DataSet();
        adapter.Fill(result);
        
        return result;
      }
    }
    
    public static string GetConnectionString() {
      return "Data Source=localhost;Initial Catalog=centros_development;User Id=centros;Password=centros";
    }
  
  }

}